//
//  WBHomeViewController.swift
//  weibo
//
//  Created by z先生 on 17/1/5.
//  Copyright © 2017年 SZT. All rights reserved.
//

import UIKit

//全局常量，否则到处都可以访问
private let cellId = "cellId"

private var statusList = [String]()

class WBHomeViewController: WBBaseViewController {

   
    
    
    
    override func loadData() {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let params = ["access_token":"2.00Ygky1G0mRuIre6baae8f43pbASyB"]
        
        WBNetworkManager.shared.get(urlString, parameters: params, progress: nil, success: { (_, json) in
            
            print(json)
            
            }) { (_, error) in
                
                print("网络请求失败\(error)")
                
        }
        for i in 0..<15 {
            
            if self.isPullup {
                
                //将数据追加到底部
                statusList.append("上拉\(i)")
                
            }else{
            statusList.insert(i.description, at: 0)
            }
        }
        
        self.refreshControl?.endRefreshing()
        
        self.isPullup = false
        
        self.tableView?.reloadData()
    }

   @objc func showFriends() {
    
    
        print(#function)
    
    let vc = WBDemoViewController()
    
//    vc.hidesBottomBarWhenPushed = true
    
    navigationController?.pushViewController(vc, animated: true)
    
    }
    
}

//表格数据源方法
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusList.count
        
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = statusList[indexPath.row]
        
        return cell
        
    }
}
extension WBHomeViewController {
    
    override func setupTableView() {
       
        
        super.setupTableView()
        
//        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
      
//        let btn = UIButton.cz_textButton("好友", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
//        
//        btn?.addTarget(self, action: #selector(showFriends), for: .touchUpInside)
//        
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn!)
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}
